package api;

import Dao.BlogDao;
import Dao.User;
import common.Common;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/delete")
public class DeleteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doDelete(req,resp);
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");

        User user = Common.checkLoginStatus(req);
        if(user == null){
            resp.getWriter().write("<script>alert('当前未登录')</script>");
            return;
        }

        String blogId = req.getParameter("blogId");
        if(blogId == null || "".equals(blogId)){
            resp.getWriter().write("<script>alert('blogId丢失')</script>");
            return;
        }
        BlogDao blogDao = new BlogDao();
        blogDao.delete(Integer.parseInt(blogId));
        resp.sendRedirect("home.html");
    }
}
